﻿using BCCommon;
using BCEntity.Sim.Admin.AdminOperationRecord;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BCData.Sim.Admin.AdminOperationRecord
{
    public class AdminOperationRecordData : IAdminOperationRecordData
    {
        protected readonly IDatabaseContext databaseContext;

        private const string SQL_INSERT_ITEM = @"INSERT INTO AdminOperationRecord(OperationName, Content, AdminId, LoginName, Fullname, CreateTime, CompanyId) VALUES (?OperationName, ?Content, ?AdminId, ?LoginName, ?Fullname, ?CreateTime, ?CompanyId)";
        private const string SQL_SELECT_ITEM_BY_ADMINOPERATIONRECORDID = @"SELECT * FROM AdminOperationRecord WHERE AdminOperationRecordId = ?AdminOperationRecord";
        private const string SQL_SELECT_ITEMS = @"SELECT * FROM AdminOperationRecord";

        public AdminOperationRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public void Create(string operationName, string content, long adminId, string loginName, string fullname, int companyId)
        {
            DynamicParameters parameters = new DynamicParameters();

            parameters.Add("?OperationName", operationName, DbType.AnsiString, size: 100);
            parameters.Add("?Content", content, DbType.AnsiString, size: 5000);
            parameters.Add("?AdminId", adminId, DbType.Int64);
            parameters.Add("?LoginName", loginName, DbType.AnsiString, size: 20);
            parameters.Add("?Fullname", fullname, DbType.AnsiString, size: 20);
            parameters.Add("?CreateTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?CompanyId", companyId, DbType.Int32);
            this.databaseContext.Master.Execute(SQL_INSERT_ITEM, parameters, databaseContext.Transaction);
        }

        public AdminOperationRecordEntity GetAdminOperationRecord(string adminOperationRecordId)
        {
            DynamicParameters parameters = new DynamicParameters();

            parameters.Add("?AdminOperationRecord", adminOperationRecordId, DbType.Int64);

            return databaseContext.Slave.QueryFirstOrDefault<AdminOperationRecordEntity>(SQL_SELECT_ITEM_BY_ADMINOPERATIONRECORDID, parameters);
        }

        public IEnumerable<AdminOperationRecordEntity> GetAdminOperationRecords(int index, int size, out int dataCount, string operationName, long? adminId, DateTime? start, DateTime? end, int? companyId)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (!string.IsNullOrWhiteSpace(operationName))
            {
                wheres.Add("OperationName LIKE CONCAT('%',CONCAT(?OperationName,'%'))");
                parameters.Add("?OperationName", operationName, DbType.AnsiString);
            }

            if (adminId.HasValue)
            {
                wheres.Add("AdminId = ?AdminId");
                parameters.Add("?AdminId", adminId, DbType.Int64);
            }

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value.Date, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value.AddDays(1).Date, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value.Date, DbType.DateTime);
                parameters.Add("?End", end.Value.AddDays(1).Date, DbType.DateTime);
            }
            if (companyId.HasValue)
            {
                wheres.Add("CompanyId = ?CompanyId");
                parameters.Add("?CompanyId", companyId.Value, DbType.Int32);
            }
            string sql = string.Format("{0} {1} ORDER BY CreateTime DESC LIMIT {2}, {3}; SELECT COUNT(1) FROM AdminOperationRecord {1}",
                SQL_SELECT_ITEMS,
                wheres.Count > 0 ? "WHERE " + string.Join(" AND ", wheres) : string.Empty,
                (index == 0 ? 0 : index - 1) * size,
                size);

            var results = databaseContext.Slave.QueryMultiple<AdminOperationRecordEntity, int>(sql,parameters);

            dataCount = results.Item2;

            return results.Item1;
        }
    }
}
